package com.ruoyi.basicData.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.basicData.mapper.CustomerListMapper;
import com.ruoyi.basicData.domain.CustomerList;
import com.ruoyi.basicData.service.ICustomerListService;

/**
 * 客户管理Service业务层处理
 * 
 * @author admin
 * @date 2024-06-27
 */
@Service
public class CustomerListServiceImpl implements ICustomerListService 
{
    @Autowired
    private CustomerListMapper customerListMapper;
    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询客户管理
     * 
     * @param customerId 客户管理主键
     * @return 客户管理
     */
    @Override
    public CustomerList selectCustomerListByCustomerId(Long customerId)
    {
        return customerListMapper.selectCustomerListByCustomerId(customerId);
    }

    /**
     * 查询客户管理列表
     * 
     * @param customerList 客户管理
     * @return 客户管理
     */
    @Override
    public List<CustomerList> selectCustomerListList(CustomerList customerList)
    {
        return customerListMapper.selectCustomerListList(customerList);
    }

    /**
     * 新增客户管理
     * 
     * @param customerList 客户管理
     * @return 结果
     */
    @Override
    public int insertCustomerList(CustomerList customerList)
    {
        customerList.setCreateTime(DateUtils.getNowDate());
        return customerListMapper.insertCustomerList(customerList);
    }

    /**
     * 批量新增客户管理
     *
     * @param customerLists 客户管理List
     * @return 结果
     */
    @Override
    public int batchInsertCustomerList(List<CustomerList> customerLists)
    {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(customerLists)) {
            try {
                for (int i = 0; i < customerLists.size(); i++) {
                    int row = customerListMapper.insertCustomerList(customerLists.get(i));
                    // 防止内存溢出，没100次提交一次,并清除缓存
                    boolean bool = (i >0 && i%3 == 0) || i == customerLists.size() - 1;
                    if (bool){
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            }catch (Exception e){
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            }finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }


    /**
     * 修改客户管理
     * 
     * @param customerList 客户管理
     * @return 结果
     */
    @Override
    public int updateCustomerList(CustomerList customerList)
    {
        customerList.setUpdateTime(DateUtils.getNowDate());
        return customerListMapper.updateCustomerList(customerList);
    }

    /**
     * 批量删除客户管理
     * 
     * @param customerIds 需要删除的客户管理主键
     * @return 结果
     */
    @Override
    public int deleteCustomerListByCustomerIds(Long[] customerIds)
    {
        return customerListMapper.deleteCustomerListByCustomerIds(customerIds);
    }

    /**
     * 删除客户管理信息
     * 
     * @param customerId 客户管理主键
     * @return 结果
     */
    @Override
    public int deleteCustomerListByCustomerId(Long customerId)
    {
        return customerListMapper.deleteCustomerListByCustomerId(customerId);
    }
}
